package com.by.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.by.bean.UserInfo;
import com.by.context.UserContext;
import com.by.entity.bo.*;
import com.by.entity.dto.Spu;
import com.by.entity.dto.SpuSearch;
import com.by.entity.vo.SpuSearchRs;
import com.by.mapper.*;
import com.by.service.ProductService;
import com.by.utils.AjaxResult;
import com.by.utils.Assert;
import com.by.utils.ExceptionResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;

    @Autowired
    SpuDetailMapper spuDetailMapper;

    @Autowired
    SkuMapper skuMapper;

    @Autowired
    SkuStockMapper skuStockMapper;

    @Autowired
    SpuExtensionMapper spuExtensionMapper;

    @Autowired
    AttrMapper attrMapper;

    @Autowired
    AttrValueMapper attrValueMapper;

    @Autowired
    SpuAttrValueMapper spuAttrValueMapper;

    @Autowired
    SpuSkuArrtValueMapper spuSkuArrtValueMapper;

    @Autowired
    SpuSearchMapper spuSearchMapper;

    @Transactional
    public AjaxResult saveProduct(Spu spu){

//        首先获取token中的用户信息
        UserInfo info= UserContext.get();
        UserContext.clear();

//        保存spu
        spu.setShopId(info.getShopId());
        int rs = productMapper.insert(spu);
        Assert.isZero(rs, ExceptionResult.SPU_MSG);

//        保存spu detail
        SpuDetail spuDetail=new SpuDetail();
        spuDetail.setSpuId(spu.getSpuId());
        spuDetail.setDetail(spu.getDetail());

        int rs1=spuDetailMapper.insert(spuDetail);
        Assert.isZero(rs1, ExceptionResult.SPU_DETAIL_MSG);

        int totalStock=0;

        //         保存规格

        for (Attr attr:spu.getAttrList()) {
            attr.setShopId(info.getShopId());
            int rs5= attrMapper.insert(attr);
            Assert.isZero(rs5,ExceptionResult.ATTR_MSG);
            Integer attrId = attr.getAttrId();
//            保存规格属性
            for (AttrValue attrValue:attr.getAttrValueList()) {
                attrValue.setAttrId(attrId);
                int rs6 = attrValueMapper.insert(attrValue);
                Assert.isZero(rs6,ExceptionResult.ATTR_VALUE_MSG);

//                保存商品规格属性关联信息
                if (attr.getAttrType()==0){
                    SpuAttrValue spuAttrValue=new SpuAttrValue();
                    spuAttrValue.setSpuId(spu.getSpuId());
                    spuAttrValue.setAttrId(attr.getAttrId());
                    spuAttrValue.setAttrName(attr.getName());
                    spuAttrValue.setAttrValueId(attrValue.getAttrValueId());
                    spuAttrValue.setAttrValueName(attrValue.getValue());
                    spuAttrValue.setAttrDesc(attr.getDescr());
                    int rs7 = spuAttrValueMapper.insert(spuAttrValue);
                    Assert.isZero(rs7,ExceptionResult.SPU_ATTR_VALUE);

                }

                //        保存sku
                for (Sku sku:spu.getSkuList()) {
                    sku.setSpuId(spu.getSpuId());
                    int rs2 = skuMapper.insert(sku);
                    Assert.isZero(rs2, ExceptionResult.SKU_MSG);
//            保存sku库存
                    totalStock+=sku.getStock();
                    SkuStock skuStock=new SkuStock();
                    skuStock.setSkuId(sku.getSkuId());
                    skuStock.setStock(sku.getStock());
                    skuStock.setActualStock(sku.getStock());
                    int rs3 = skuStockMapper.insert(skuStock);
                    Assert.isZero(rs3, ExceptionResult.SKU_STOCK_MSG);

//                    保存商品sku规格属性关联信息
                    if (attr.getAttrType()==1){
                        SpuSkuAttrValue spuSkuAttrValue=new SpuSkuAttrValue();
                        spuSkuAttrValue.setSpuId(spu.getSpuId());
                        spuSkuAttrValue.setSkuId(sku.getSkuId());
                        spuSkuAttrValue.setAttrId(attr.getAttrId());
                        spuSkuAttrValue.setAttrName(attr.getName());
                        spuSkuAttrValue.setAttrValueId(attrValue.getAttrValueId());
                        spuSkuAttrValue.setAttrValueName(attrValue.getValue());
                        int rs9 = spuSkuArrtValueMapper.insert(spuSkuAttrValue);
                        Assert.isZero(rs9,ExceptionResult.SPU_SKU_ATTR_VALUE);
                    }

                }

//        保存spu总库存
                SpuExtension spuExtension=new SpuExtension();
                spuExtension.setSpuId(spu.getSpuId());
                spuExtension.setActualStock(totalStock);
                spuExtension.setStock(totalStock);

                int rs4 = spuExtensionMapper.insert(spuExtension);
                Assert.isZero(rs4,ExceptionResult.SPU_STOCK_MSG);

            }

        }

        return AjaxResult.success();

    }
//分页查询商品
    @Override
    public AjaxResult searchProduct(int pageNo, int pageSize, SpuSearch spuSearch) {
//        获取用户信息
        UserInfo info= UserContext.get();
        UserContext.clear();
//开启分页
        PageHelper.startPage(pageNo, pageSize);
//查询结果
        spuSearch.setShopId(info.getShopId());
        List<SpuSearchRs> list = spuSearchMapper.searchProduct(spuSearch);
        PageInfo<Map> pageInfo=new PageInfo(list);





        return AjaxResult.success(pageInfo);
    }
}
